package il.technion.cs236369.proxy;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.Properties;

import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;

import com.google.inject.Scopes;
import com.google.inject.name.Names;

public class HttpProxyTestModule extends HttpProxyModule {

	private final ServerSocketFactory mockedServerSocketFactory = mock(ServerSocketFactory.class);
	private final SocketFactory mockedSockFactory = mock(SocketFactory.class);
	private final ServerSocket mockedServerSocket = mock(ServerSocket.class);

	public HttpProxyTestModule() {
	}

	public HttpProxyTestModule(Properties properties) {
		super(properties);
	}

	public ServerSocketFactory getMockedServerSocketFactory() {
		return mockedServerSocketFactory;
	}

	public SocketFactory getMockedSockFactory() {
		return mockedSockFactory;
	}

	public ServerSocket getMockedServerSocket() {
		return mockedServerSocket;
	}

	@Override
	protected void configure() {
		Names.bindProperties(binder(), super.properties);
		bind(ServerSocketFactory.class).toInstance(
				this.getMockedServerSocketFactory());
		bind(SocketFactory.class).toInstance(this.getMockedSockFactory());

		try {
			when(mockedServerSocketFactory.createServerSocket(anyInt()))
					.thenReturn(mockedServerSocket);
		} catch (IOException e) {
			throw new AssertionError();
		}

		bind(IHttpProxy.class).to(HttpProxy.class).in(Scopes.SINGLETON);
	}

}
